package com.guo.boot.controller;

import com.guo.boot.api.CommonResult;
import com.guo.boot.entity.User;
import com.guo.boot.mapper.UserMapper;
import com.guo.boot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author HeartGuo
 * @date 2021/12/19 0019 - 18:51
 */
@RequestMapping("/boot")
@ResponseBody
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @Resource
    private UserMapper userMapper;

    //登录
    @RequestMapping("/login")
    public CommonResult login(@RequestBody User user){
        String username = user.getUsername();
        String password = user.getPassword();
        user = userService.login(username,password);
        if(user != null){
        		return CommonResult.success("guo,2021/12/19,登录成功");
        }else{
        		return CommonResult.validateFailed("登录失败");
        }
    }

    //注册
    @RequestMapping("/register")
    public CommonResult register(@RequestBody User user){
        if(user.getUsername()==null||user.getUsername()==""){
            return CommonResult.validateFailed("用户名为空，请输入用户名");
        }else if(userMapper.selectByUname(user.getUsername())!=null){
            return CommonResult.validateFailed("用户名已存在，请更换用户名");
        }else if(user.getPassword()==null||user.getPassword()==""){
            return CommonResult.validateFailed("密码为空，请输入密码");
        }
        int resultUser = userService.register(user);
        if(resultUser>0){
            return CommonResult.success("注册成功");
        }else {
            return CommonResult.validateFailed("注册失败");
        }
    }
}
